package com.example.campuslink.ui.activity

import android.text.Editable
import android.text.TextWatcher
import com.example.campuslink.BR
import com.example.campuslink.R
import com.example.campuslink.commen.tools.putExtraKey
import com.example.campuslink.databinding.ActivityWordsBinding
import com.example.campuslink.viewModel.WordEditViewModel

class WordsActivity : BaseActivity<ActivityWordsBinding, WordEditViewModel>(
    WordEditViewModel::class.java,
    R.layout.activity_words,
    BR.viewModel
) {
    override fun initView() {
        val word = intent.getStringExtra(putExtraKey.WORD.key)
        mViewDataBinding?.etSay?.setText(word ?: "")
        onClick()
        mViewDataBinding?.etSay?.setSelection(mViewDataBinding?.etSay?.text?.length ?: 0)
//        mViewModel?.wordSize?.observe(this) {
//            mViewDataBinding?.etSay?.setSelection(it)
//        }
    }

    fun onClick() {
        mViewDataBinding?.ivBack?.setOnClickListener {
            finish()
        }
        mViewDataBinding?.btnNext?.setOnClickListener({
            mViewModel?.jumpToWordEditActivity(mViewDataBinding?.etSay?.text.toString())
        })
        mViewDataBinding?.etSay?.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(p0: Editable?) {
                mViewModel?.setWordSize(p0?.length ?: 0)
            }

        })
    }
}